
import { login } from "@/api/api";
import { ElMessage } from 'element-plus';

export default ({fromRef,ruleForm}) => {
  const ruleFormRef = ref( fromRef );
  let router = useRouter();
  let store = useStore();
  // 登录按钮
  const submitForm = async ( formEl ) => {
    console.log(formEl);
    if (!formEl) return
    await formEl.validate( async (valid, fields) => {
      if ( valid ) {
        let {data, meta} = await login( ruleForm );
        if (meta.status == 200) {
          ElMessage({
            message: meta.msg,
            type: 'success',
          } )
          console.log(data.token);
          store.commit( 'SAVE_TOKEN', data.token );
          router.push( { path: '/home' } );
        } else {
          ElMessage({
            message: meta.msg,
            type: 'error',
          })
        }

      } else {
        console.log('error submit!', fields)
      }
    })
  }
  return {
    ruleFormRef,
    submitForm
  }
}